package com.huluobo.a2111douyindemo.register;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2111douyindemo.R;
import com.huluobo.a2111douyindemo.databinding.ActivityRegisterBinding;
import com.huluobo.mvvm_base.mvvm.base.view.BaseMVVMActivity;
import com.huluobo.mvvm_base.mvvm.bean.UserBeanResult;

public class RegisterActivity extends BaseMVVMActivity<ActivityRegisterBinding, RegisterViewModel> {

    @Override
    protected void initData() {
        viewModel.registerSuccess.observe(this, new Observer<UserBeanResult>() {
            @Override
            public void onChanged(UserBeanResult userBeanResult) {
                if (userBeanResult.getCode() == 200) {
                    ToastUtils.showLong(userBeanResult.getMessage());
                    finish();
                } else {
                    ToastUtils.showLong(userBeanResult.getMessage());
                }
            }
        });

        viewModel.registerFailed.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
    }

    @Override
    protected void initView() {
        dataBinding.registerToRegister.setOnClickListener(v -> {
            register();
        });
    }

    private void register() {
        String usernameString = dataBinding.registerUsername.getText().toString().trim();
        String passwordString = dataBinding.registerPassword.getText().toString().trim();
        String rePasswordString = dataBinding.registerRePassword.getText().toString().trim();
        if (TextUtils.isEmpty(usernameString) || TextUtils.isEmpty(passwordString) || TextUtils.isEmpty(rePasswordString)) {
            ToastUtils.showLong("请输入用户名和密码");
            return;
        }
        if (!TextUtils.equals(passwordString, rePasswordString)) {
            ToastUtils.showLong("请确认两次密码输入一致");
            return;
        }
        viewModel.register(usernameString, passwordString, rePasswordString);
    }

    @Override
    protected Class<RegisterViewModel> getViewModel() {
        return RegisterViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }
}